<?php

/**
 * Created by PhpStorm.
 * User: kc
 * Date: 2017/3/3
 * Time: 15:41
 */
class AhpModuleBase extends Eloquent
{
    protected $table = 'ahp_module_base';

    public static $module_type = array(
        '1' => '轮播图',
        '0' => '其他',
    );
    public function ahp_user(){
        return $this->hasOne('ahp_user','id','created_by');
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-03-06 14:42:29
     * @Description: 模块列表
     * @Param: $number 模块编号
     * @Param: $name 模块名称
     * @Param: $start_time 开始时间
     * @param: $end_time 结束时间
     * @param: $page 第几页
     * @param: $page_size 每页多少条
     * @Return: Object
     */
    public static function getModule($number=null,$name=null,$start_time=null,$end_time=null,$type=null,$page,$page_size){
        $ahp_module = AhpModuleBase::where('status',1);
        if(!empty($number)){
            $ahp_module->where('mod_number','like','%'.$number.'%');
        }
        if(!empty($name)){
            $ahp_module->where('mod_name','like','%'.$name.'%');
        }
        if(!empty($type)){
            $ahp_module->where('mod_type',$type);
        }
        if(!empty($start_time) && !empty($end_time)){
            $ahp_module->whereBetween('created_at',[$start_time.' 00:00:00',$end_time.' 23:59:59']);
        }
        $count = $ahp_module->count();
        $data = $ahp_module->orderBy('created_at','desc')->skip($page_size*($page-1))->take($page_size)->get();
        foreach($data as $v){
            $v->created_by = AhpUsers::where('id',$v->created_by)->pluck('user_name');
        }
        $module = array(
            'moduleList' => $data,
            'count' => $count,
        );
        return $module;
    }
    /**
     * @Author:      kc
     * @DateTime:    2017-03-06 15:27
     * @Description: 模板详情
     * @Param: $module_id 模板ID
     * @Return: Object
     */
    public static function getModuleDetails($module_id){
        $data = AhpModuleBase::where('id',$module_id)->where('status',1)->first();
        $data->type_name = isset(AhpModuleBase::$module_type[$data->type])?AhpModuleBase::$module_type[$data->type]:'';
        return $data;
    }
}